package com.lp.mgt.services.impls;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lp.mgt.entities.Post;
import com.lp.mgt.entities.User;
import com.lp.mgt.mappers.PostMapper;
import com.lp.mgt.services.PostService;
import com.lp.mgt.utils.ShrioUtil;
import com.lp.mgt.utils.UUIDUtil;
@Service
public class PostServiceImpl implements PostService {
	@Autowired
	private PostMapper postMapper;
	
	@Override
	public List<Post> getPostsByDeptId(String deptId) {
		return postMapper.getPostsByDeptId(deptId);
	}

	@Override
	public void removePostByUserId(String uid) {
		 postMapper.removePostByUserId(uid);
	}

	@Override
	public Map<String, Object> listPostsWithTerms(Post post) {
		Map<String,Object> map=new HashMap<>();
		PageHelper.startPage(post.getPage(), post.getLimit());
		List<User> list=postMapper.listPostsWithTerms(post);
		PageInfo<User> pageInfo = new PageInfo<>(list);
		map.put("total",pageInfo.getTotal());
		map.put("rows", pageInfo.getList());
		System.out.println(pageInfo.getList());
		return map;
	}

	@Override
	public void savePost(Post post) {
		if(post!=null) {
			post.setPostId(UUIDUtil.getUUID());
			post.setCreateTime(new Date());
			post.setCreateBy(ShrioUtil.getCurrentUser().getUid());
			postMapper.savePost(post);
			postMapper.savePostAndDept(post);
		}
	}

	@Override
	public void removePostByPostId(String postId) {
		postMapper.removePostByPostId(postId);
		//删除关联表数据
		postMapper.removePostAndDeptByPostId(postId);
		postMapper.removePostAndUserByPostId(postId);
	}

	@Override
	public void editPost(Post post) {
		postMapper.editPost(post);
	}

	@Override
	public void removePostByDeptId(String deptId) {
		postMapper.removePostByDeptId(deptId);
	}

}
